/* 
 * File:   MoKnetG.h
 * Author: CreMindES
 *
 * Created on 2012. szeptember 16., 10:21
 */

#ifndef MOKNETG_H
#define	MOKNETG_H


#define _XTAL_FREQ                          16000000
// TODO: calculate normal value!!
#define RTU_DELAY                           256-_XTAL_FREQ/256/256/4

#define MODBUS_RX_LED_ON()                  LATD0 = 1
#define MODBUS_RX_LED_OFF()                 LATD0 = 0
#define MODBUS_TX_LED_ON()                  LATD1 = 1
#define MODBUS_TX_LED_OFF()                 LATD1 = 0
#define KNET_RX_LED_ON()                    LATD2 = 1
#define KNET_RX_LED_OFF()                   LATD2 = 0
#define KNET_TX_LED_ON()                    LATD3 = 1
#define KNET_TX_LED_OFF()                   LATD3 = 0

#define DISABLE_MODBUS_RECEPTION()          PIE1bits.RC1IE = 0
#define ENABLE_MODBUS_RECEPTION()           PIE1bits.RC1IE = 1
#define DISABLE_KNET_RECEPTION()            PIE3bits.RC2IE = 0
#define ENABLE_KNET_RECEPTION()             PIE3bits.RC2IE = 1

// Prototypes

// Device specific functions
void initDevice(void);
void readConfiguration(void);
void initTimer0(void);
void initTimer1(void);
//void initTimer2(void);
void turnLEDsOff(void);
void resetDeviceArray(void);

void interrupt myISR(void);

void startTimeoutCheckService(void);        // TODO: finish implementing
void stopTimeoutCheckService(void);
void resetCommunication(void);


// MODBUS functions
void initModbusUSART(void);
void switchToModbusTx(void);
void switchToModbusRx(void);
void receiveModbusData(void);
void sendModbusData(void);

unsigned char validateAsciiMessage(void);
unsigned char validateRtuMessage(void);

void readModbusAsciiMessage(void);
void readModbusRtuMessage(void);

unsigned char getMessageDataType(void);
float readModbusMessageDataValueFloat(char num);
unsigned char validateModbusMessage(void);
void sendExceptionResponse(unsigned char error);

unsigned char convertMessageDataAddress(unsigned char messagaDataType);
unsigned char getMessageDataType(void);

void modbusMessageToKnet(void);


void updateDeviceArrayTwo(void);
void updateDeviceArrayThree(void);

// K-Net functions
void initKnetUSART(void);
void switchToKnetTx(void);
void switchToKnetRx(void);
void receiveKnetData(void);
void sendKnetData(void);

unsigned char validateKnetMessage(void);
int searchKnetMessageColon(void);
void readKnetMessage(void);
void readKnetMessageAddress(void);
void readKnetDataValue(void);
unsigned char searchKnetMessageComma(unsigned char i);
void readKnetDataValueFloat(void);

void knetToModbusAscii(void);
void knetToModbusRtu(void);

// LRC, CRC
unsigned char generateLRC(unsigned char len, unsigned char *buffer);
unsigned int generateCRC(unsigned char len, unsigned char *buffer);

// Converter functions
char convertDecToHexInAsciiHi(int num);
char convertDecToHexInAsciiLo(int num);
int dataToDec(int sorszam);
int convertAsciiToDec(char c);
char convertDecToAscii(int num);


enum transmissionMode {
    ASCII_MODE,             // 0
    RTU_MODE                // 1
};

enum trasnmissionState {
    IDLE_STATE,             // 0
    RECEIVING_STATE,        // 1
    ERROR_STATE             // 2
};

enum modbusBaudRateOptions {
    B_2400  = (_XTAL_FREQ/64/2400)-1,
    B_9600  = (_XTAL_FREQ/64/9600)-1,
    B_19200 = (_XTAL_FREQ/64/19200)-1,
    B_38400 = (_XTAL_FREQ/64/38400)-1
};

// Variables
enum messageParts {
    DEVICE_ADDRESS,         // 0
    FUNCTION,               // 1
    REGISTER_ADDRESS,       // 2
    DATA_COUNT,             // 3
    IS_DATA_FLOAT,          // 4
    DATA_VALUE_INT,         // 5
    MESSAGE_PART_NUM        // 6
};

enum eKnetRegisterType {
    UNIVERSAL_INPUT,        // 0
    DIGITAL_INPUT,          // 1
    ANALOG_OUTPUT,          // 2
    RELAY_OUTPUT,           // 3
    NUMERIC,                // 4
                            // there is no 5
    TIMER = 6,              // 6
    SYSTEM_VARIABLE = 7     // 7
};

enum eExceptionResponse {
    ILLIGAL_FUNCTION = 1,   // 1
    ILLIGAL_DATA_ADDRESS,   // 2
    ILLIGAL_DATA_VALUE,     // 3
    SLAVE_DEVICE_FAILURE,   // 4
    ACKNOWLEDGE,            // 5
    SLAVE_DEVICE_BUSY,      // 6
    NEGATIVE_ACKNOWLEDGE,   // 7
    MEMORY_PARITY_ERROR     // 8
};

unsigned char transmissionState = IDLE_STATE;

// TODO enum?
unsigned char rtuMessageAvailable = 0;

unsigned char modbusBaudRate=0;
unsigned int timeoutCount = 0;

#endif	/* MOKNETG_H */



